package com.example.user.userinfo

import androidx.lifecycle.viewModelScope
import com.example.lib_base.entity.Res
import com.example.lib_base.entity.SendCodeEntity
import com.example.lib_base.entity.UserInfoEntity
import com.example.lib_base.mvvm.BaseViewModel
import com.example.lib_base.utils.SingleLiveData
import kotlinx.coroutines.launch

class UserInfoViewModel:BaseViewModel() {

    val userInfoSuccess = SingleLiveData<Res<UserInfoEntity>>()
    val userInfoFail = SingleLiveData<String>()

    fun userInfo(){
        viewModelScope.launch {
            try {
                userInfoSuccess.postValue(apiService.userInfo())
            } catch (e: Exception) {
                userInfoFail.postValue(e.message.toString())
            }
        }
    }

    //发送验证码
    val sendCodeSuccess = SingleLiveData<Res<SendCodeEntity>>()
    val sendCodeFail = SingleLiveData<String>()
    fun sendCode(phone:String){
        viewModelScope.launch {

            try {
                sendCodeSuccess.postValue(apiService.sendCode(phone))
            } catch (e: Exception) {
                sendCodeFail.postValue(e.message.toString())
            }
        }
    }
    //修改密码
    val updatePwdSuccess = SingleLiveData<Res<Boolean>>()
    val updatePwdFail = SingleLiveData<String>()
    fun updatePwd(account:String,captcha:String,password:String){
        viewModelScope.launch {
            try {
                val map = mutableMapOf<String,Any>()
                map["account"] = account
                map["captcha"] = captcha
                map["password"] = password
                updatePwdSuccess.postValue(apiService.updatePwd(mapToBody(map)))
            } catch (e: Exception) {
                updatePwdFail.postValue(e.message.toString())
            }
        }

    }
}